package com.qys.training.server.controller;

import com.qys.training.base.dto.BaseResult;
import com.qys.training.base.util.CacheUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/cache")
public class CacheController {

    public static final Logger logger = LoggerFactory.getLogger(CacheController.class);

    private final String DOMAIN = "CACHETS";

    @Autowired
    private CacheUtils cacheUtils;

    /**
     * 存值
     * @param key
     * @param value
     * @param time
     * @return
     */
    @GetMapping("/add")
    public @ResponseBody
    BaseResult add(String key, String value,Long time) {
        if (null != time) {
            return BaseResult.success(cacheUtils.add(DOMAIN,key,value,time, TimeUnit.SECONDS));
        }
        return BaseResult.success(cacheUtils.add(DOMAIN,key,value));
    }

    /**
     * 取值
     * @param key
     * @return
     */
    @GetMapping("/get")
    public @ResponseBody
    BaseResult get(String key)  {
        return BaseResult.success(cacheUtils.get(DOMAIN, key));
    }


    /**
     * 删除
     * @param keys
     * @return
     */
    @GetMapping("/delete")
    public @ResponseBody
    BaseResult delete(String ...keys)  {
        cacheUtils.del(DOMAIN, keys);
        return BaseResult.success();
    }

}
